package com.jic.data.service.impl;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.jic.data.constant.consist.ResponseCode;
import com.jic.data.exception.AppRuntimeException;
import com.jic.data.model.entity.SaleSingleDetail;
import com.jic.data.model.vo.response.MonOneMoreGuestDetailCategoryResponse;
import com.jic.data.model.vo.response.MonSaleSingleDetailResponse;
import com.jic.data.model.vo.response.SaleDetailResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import javax.annotation.Resource;
import java.math.BigDecimal;
import com.jic.data.model.vo.response.SaleSingleSaleDetailResponse;
import com.jic.data.model.entity.SaleSingleSaleDetail;
import com.jic.data.mapper.SaleSingleSaleDetailMapper;
import com.jic.data.service.SaleSingleSaleDetailService;

import lombok.extern.slf4j.Slf4j;
/**
 * 接口实现类
 *
 * @author : 建投数据科技(山东)有限公司
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-09 10:12:20
 */
@Service
@Slf4j
public class SaleSingleSaleDetailServiceImpl implements SaleSingleSaleDetailService {

	@Resource
	private SaleSingleSaleDetailMapper mapper;



	/**
	 * 月度一次客回购客对比
	 *
	 * @param request 不分页查询入参实体类
	 * @return 数据库查到的实体类列表
	 */
	@Override
	public List<SaleSingleSaleDetailResponse> listAllRecord(SaleSingleSaleDetail request) {
		List<SaleSingleSaleDetail> list = mapper.selectBySelective(request);

		List<SaleSingleSaleDetailResponse> responseList = list.stream().map(temp -> {
			SaleSingleSaleDetailResponse bo = new SaleSingleSaleDetailResponse();
			Date saleDate = temp.getSaleDate();
			temp.setSaleDate(null);
			BeanUtils.copyProperties(temp, bo);
			bo.setSaleDate(DateUtil.format(saleDate, "yyyy年MM月"));
			return bo;
		}).filter(item -> item != null).collect(Collectors.toList());

		return responseList;
	}


	/**
	 * 月度商品单品销售明细
	 * @param request
	 * @return
	 */
	public List<SaleDetailResponse<List<MonSaleSingleDetailResponse>>> listProductSaleDetail(SaleSingleSaleDetail request) {
		List<SaleDetailResponse<List<MonSaleSingleDetailResponse>>> responseList = new ArrayList<>();
		List<SaleSingleSaleDetail> list = mapper.selectBySelective(request);
		Map<Date, List<SaleSingleSaleDetail> > groupby = list.stream().collect(Collectors.groupingBy(SaleSingleSaleDetail :: getSaleDate));
		groupby.keySet().forEach(key -> {
			SaleDetailResponse<List<MonSaleSingleDetailResponse>> responses = new SaleDetailResponse();
			responses.setDate(DateUtil.format(key,"yyyy年MM月"));
			List<SaleSingleSaleDetail> groupDatas = groupby.get(key);

			List<MonSaleSingleDetailResponse> groupList = groupDatas.stream().map(temp -> {
				MonSaleSingleDetailResponse bo = new MonSaleSingleDetailResponse();
				BeanUtils.copyProperties(temp, bo);
				return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
			responses.getList().add(groupList);

			responseList.add(responses);
		});
		return responseList;
	}

}
